package pww.dao.studentImpl;

import a.annotation.Component;
import dbUtil.SQLExecteException;
import dbUtil.handler.BeanHandler;
import pww.dao.StudentDao;
import pww.entity.Student;
import pww.utils.ConnUtil;
import dbUtil.ResultsetHandler;
import dbUtil.SQLExecute;
import dbUtil.handler.BeanListHandler;
import pww.utils.SQlExcuteFactory;

import java.sql.Connection;
import java.util.List;
@Component
public class StudentDaoImpl implements StudentDao {
    //查询所有的学生信息
    public List<Student> queryAllStudent() {
        Connection connection = ConnUtil.getConnection();
        String sql="select * from student";
        ResultsetHandler<List<Student>> handler=new BeanListHandler<Student>(Student.class);
        SQLExecute sqlExecute=new SQLExecute(connection);
        List<Student> students = sqlExecute.executeQuery(sql, handler);
        return students;
    }
    //添加学生信息
    @Override
    public void insertStudent(Student student,SQLExecute sqlExecute) {
            String sql="insert into student(name,age,addr,sex,classes,loving) values(?,?,?,?,?,?)";
             sqlExecute = SQlExcuteFactory.openSqlExcute();
            sqlExecute.executeUpdate(sql,student.getName(),student.getAge(),student.getAddr(),student.getSex(),student.getClasses(),student.getLoveing());
    }
    //查询是否有相同昵称
    @Override
    public Student find(String name,SQLExecute sqlExecute) {
        String sql="select * from student where name=?";
        ResultsetHandler<Student> handler=new BeanHandler<Student>(Student.class);
        Student student1 = sqlExecute.executeQuery(sql, handler,name);
        return student1;
    }
}
